package Main;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;

public class Clause {
	
	// sort and remove duplicates
	public static int [] formatList(int [] list) {
		Arrays.sort(list);
		int [] tempList = new int[list.length];
		
		int j = 1, i = 1, temp = list[0];
		tempList[0] = temp;
		
		while(i < list.length) {
			if(temp != list[i]) {
				temp = list[i];
				tempList[j] = temp;
				j++;
			}
			i++;
		}
		
		int [] newList = new int[j];
		for(i = 0; i < newList.length; i++){
			newList[i] = tempList[i];
		}
		return newList;
	}
	
	public static boolean contains(int [] list, int var) {
		if(list.length < 20) {
			for(int i = 0; i < list.length; i++)  {
				if(list[i] == var) {
					return true;
				}
			}
			return false;
		} else {
			if(Arrays.binarySearch(list, var) > 0 ) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	public static boolean willFalsify(int [] list, int var) {
		if(list.length == 1 && list[0] == -var) {
			return true;
		} else {
			return false;
		}
	}
	
	public static ArrayList<int[]> getNextClauses(ArrayList<int[]> clauses, int var) {
		ArrayList<int[]> newClauses = new ArrayList<int[]>();
		int [] list, tempList;
		for(int i = 0; i < clauses.size(); i++) {
			tempList = clauses.get(i);
			if(Clause.willFalsify(tempList, var)) {
				return null;
			} else {
				list = Clause.getNextList(tempList, var);
				if(list != null) {
					newClauses.add(list);
				}
			}
		}
		return newClauses;
	}
	
	public static int[] getNextList(int [] list, int var) {
		if(contains(list, var)) {
			return null;
		} else if(!contains(list, -var)) {
			return list;
		} else {
			int [] newList = new int[list.length - 1];
			int j = 0, i = 0;
			while(i < list.length) {
				if(list[i] != -var) {
					newList[j] = list[i];
					j++;
				}
				i++;
			}
			return newList;
		}
	}

  public static int[] clauseEliminatorChecker(int [] list, ArrayList<Integer> varRemovedClause){
	boolean isNull = false;
    for(int var:list){
      if(contains(list,-var)){
    	  if(!varRemovedClause.contains(var) && !varRemovedClause.contains(-var)) {
    		  varRemovedClause.add(var);
    		  //isNull = true;
    		  return null;
    	  }
      }
    }
    if(isNull) {
    	return null;
    } else {
    	return list;
    }
  }
  
  
  
}
